package operation;

import books.BookList;

import java.util.Scanner;

public class ReWorkOperation implements IOperation {
    public int search(BookList bookList) {
        System.out.println("请输入你要修改的图书名");
        Scanner scan = new Scanner(System.in);
        String name = scan.nextLine() ;
        int len = bookList.getBookSize() ;
        for(int i = 0 ; i < len ; i ++){
            if(bookList.books[i].getName().equals(name)) {
                return i;
            }
        }
        return -1 ;
    }
    @Override
    public void work(BookList bookList) {
     int tmp = search(bookList) ;
     if(tmp == -1 ) {
         System.out.println("没有找到你要修改的图书");
         return;
     }

        Scanner scan = new Scanner(System.in) ;


    while (true){
        System.out.println(bookList.books[tmp]);
        System.out.println("请输入你要进行的操作 : 1. 修改图书名 2.修改作者 3.修改类型 4.修改价格 0.退出修改");
        int choose = scan.nextInt() ;
        switch (choose){
            case 1:{
                System.out.println("输入新的图书名");
                scan.nextLine();
                String name = scan.nextLine() ;
                bookList.books[tmp].setName(name);
                System.out.println("修改成功");
                break ;}
            case 2:{
                System.out.println("输入新的作者名");
                scan.nextLine();
                String name = scan.nextLine() ;
                bookList.books[tmp].setAuthor(name);
                System.out.println("修改成功");
                break ;}
            case 3:{
                System.out.println("输入新的类型名");
                scan.nextLine();
                String name = scan.nextLine() ;
                bookList.books[tmp].setType(name);
                System.out.println("修改成功");
                break ;}
            case 4:{
                System.out.println("输入新的价格");
                int money = scan.nextInt();
                bookList.books[tmp].setMoney(money);
                System.out.println("修改成功");
                break ;}
            case 0:{
                System.out.println("退出修改");
               return;
                }
            default:
                System.out.println("输入错误");
                break;
        }
    }



    }
}